using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.IO;

using InfFront.UICore;

namespace InfFront.InfoSources.Base
{
    // TODO: don't use the AbstractListInfoSource here. derive directly from the InfoSource instead
    public partial class SingleObjectInfoSource : AbstractListInfoSource
    {
        #region Private Variables
        private static readonly object EVENT_GETOBJECT = new object();
        #endregion

        #region Construction/Initialization
        public SingleObjectInfoSource()
        {
            Init();
        }

        public SingleObjectInfoSource(IContainer container)
        {
            container.Add(this);
            Init();
        }

        private void Init()
        {
            base.MaxEntryCount = 1;
            InitializeComponent();
        }
        #endregion

        public class GetObjectEventArgs: EventArgs
        {
            public object Object { get; set; }
        }
        public delegate void GetObjectEventHandler(object sender, GetObjectEventArgs args);

        public event GetObjectEventHandler OnGetObject
        {
            add { Events.AddHandler(EVENT_GETOBJECT, value); }
            remove { Events.RemoveHandler(EVENT_GETOBJECT, value); }
        }

        [Browsable(false), DefaultValue(1)]
        public new int MaxEntryCount
        {
            get { return base.MaxEntryCount; }
        }

        protected void BeginEdit()
        {
            base.BeginEdit(0);
        }

        protected sealed override IList GetInternalList()
        {
            var args = new GetObjectEventArgs();
            var handler = (GetObjectEventHandler)Events[EVENT_GETOBJECT];
            if (handler != null)
                handler(this, args);
            return new ArrayList() { args.Object };
        }

        protected override object CreateEmptyItem()
        {
            throw new NotSupportedException();
        }
    }
}
